package com.cavas.domain.dao;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cavas.domain.bean.Finance;
import com.cavas.domain.bean.Membre;
import com.cavas.domain.bean.Pret;
import com.cavas.domain.bean.Remboursement;

@Repository
public class DaoFinanceImpl implements DaoFinance{

	@Autowired
	SessionFactory sessionFactory;
		
	public void setSessionFactory(SessionFactory session){
		this.sessionFactory = session;
	}
	
	@Override
	public void addFinance(Finance finance, int idMembre) {
		Session session = sessionFactory.getCurrentSession();
		Membre membre = (Membre) session.get(Membre.class, idMembre);
		finance.setNom(membre.getNom());
		finance.setMembre(membre);
		session.save(finance);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Finance> allFinance() {
	
		return sessionFactory.getCurrentSession().createQuery("from Finance").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Finance getFinance(String mail){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Finance fi where fi.nom =:email");
		query.setParameter("email", mail);
		List<Finance> liste = query.list();
		if(liste.size() > 0)
			return (Finance)liste.get(0);
		return null;
	}

	@Override
	public int allFdr() {
		List<Finance> liste = this.allFinance();
		int total = 0;
		
		for(Finance finance : liste){
			total = total + finance.getFdr();
		}
		return total;
	}

	@Override
	public int allInteret() {
		List<Finance> liste = this.allFinance();
		int total = 0;
		
		for(Finance finance : liste){
			total = total + finance.getInteret();
		}
		return total;
	}

	@Override
	public void calculateInteret(Remboursement rbm, Pret pret){
		Session session = sessionFactory.getCurrentSession();
		int montant = rbm.getInteret();
		String liste_p =  pret.getFinances();
		int total_p = 0;
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		String[] tp1 = liste_p.split("\\.");
		
		for(String st: tp1){
			System.out.println("kst "+st);
			Integer mail = Integer.parseInt(st.split(":")[0]);
			Integer fdr = Integer.parseInt(st.split(":")[1]);
			total_p = total_p + fdr;
			map.put(mail, fdr);
		}
				
		Set<Integer> keys = map.keySet(); 
		for(Integer key : keys ){
					Finance finance = (Finance) session.get(Finance.class, key);
					System.out.println("fdr "+finance.getFdr()+" interet "+finance.getInteret());
					int fdr = map.get(key);
					int interet = this.getInteret(total_p, fdr, montant);
					int tpp = finance.getInteret() + interet;
					finance.setInteret(tpp);
					finance.setFdr(finance.getFdr()+ tpp);
					finance.setInfo(String.valueOf(interet));
					session.update(finance);				
		}		
	}
	

	private int getInteret(int total_fdr, int fdr, int montant) {
		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
		symbols.setDecimalSeparator('.');
		DecimalFormat newFormat = new DecimalFormat("#.#", symbols);
		double temp = (double)fdr/total_fdr;
		temp = montant* temp;
		System.out.println("temp"+temp);
		double two =  Double.valueOf(newFormat.format(temp));
		String tp = String.valueOf(two);
		String[] tp2 = tp.split("\\.");
		int tp3 = Integer.parseInt(tp2[0]);
		int tp4 = Integer.parseInt(tp2[1]);
		if(tp4 > 4)
			tp3 = tp3 + 1;
		
		return tp3;
	}

	@Override
	public void deleteFinance(int id) {
		Session session = sessionFactory.getCurrentSession();
		Finance fi = (Finance) session.get(Finance.class, id);
		if(fi != null)
			session.delete(fi);
		
	}

}
